package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 搭忆长安
 * @version 1.0
 * @date 2021年06月26日 16:32
 * @ClassName:ManageServiceImpl
 * @Description: TODO
 */
@Service
@Transactional
public class ManageServiceImpl implements ManageService {
    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;
    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    private BaseAttrInfoMapper   baseAttrInfoMapper;
    @Autowired
    private BaseAttrValueMapper baseAttrValueMapper;
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;
    @Autowired
    private BaseSaleAttrMapper baseSaleAttrMapper;
    @Autowired
    private SpuImageMapper spuImageMapper;
    @Autowired
    private SpuInfoMapper spuInfoMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;

    @Autowired
    private  SkuImageMapper skuImageMapper;

    @Autowired
    private  SkuInfoMapper skuInfoMapper;
    @Autowired
    private  SkuAttrValueMapper skuAttrValueMapper;
    @Autowired
    private  SkuSaleAttrValueMapper skuSaleAttrValueMapper;
    /**
     * 查询所有一级分类列表
     * @return
     */
    @Override
    public List<BaseCategory1> getCategory1List() {
        return baseCategory1Mapper.selectList(null);
    }

    /**
     * 根据一级分类查询二级分类列表
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseCategory2> getCategory2List(Long category1Id) {
        //参数判断
        if(category1Id==null){
            throw new RuntimeException("二级分类查询失败，一级分类不能为空");
        }
        //条件构建
        LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategory2::getCategory1Id,category1Id);
        //返回二级分类列表
        return baseCategory2Mapper.selectList(wrapper);
    }

    /**
     * 根据二级分类查询三级分类列表
     * @param category2Id
     * @return
     */
    @Override
    public List<BaseCategory3> getCategory3List(Long category2Id) {
        //参数判断
        if(category2Id==null){
            throw  new RuntimeException("三级分类查询失败，二级分类不能为空");
        }
        LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategory3::getCategory2Id,category2Id);
        return baseCategory3Mapper.selectList(wrapper);
    }

    /***
     * 根据分类查询所有规格信息列表:查询1 2 3级分类对应的规格列表，若只查询某个分类下的规格列表则其他分类的值传0
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getBaseAttrInfoList(Long category1Id, Long category2Id, Long category3Id) {

        return baseAttrInfoMapper.selectBaseAttrInfoList(category1Id,category2Id,category3Id);
    }
    /**
     * 新增规格
     *
     * @param baseAttrInfo
     * @return
     */
    @Override
    public BaseAttrInfo addAttrInfo(BaseAttrInfo baseAttrInfo) {
        //参数校验
        if(baseAttrInfo==null){
            throw  new RuntimeException("参数为空");
        }
        //根据id是否已为空，判断为新增还是修改
        if(baseAttrInfo.getId()==null){
            //新增的场合insert info的信息
            baseAttrInfoMapper.insert(baseAttrInfo);
        }else{
            //修改的场合update info的信息
            baseAttrInfoMapper.updateById(baseAttrInfo);
            LambdaQueryWrapper<BaseAttrValue> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
            baseAttrValueMapper.delete(wrapper);
        }
        //获取即将保存的value表的数据
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        //方式一:保存value表数据前填充info表的id字段(效率高)
        attrValueList.stream().map(c->{
          //补全info表的id字段
          c.setAttrId(baseAttrInfo.getId());
          //保存value表数据
            baseAttrValueMapper.insert(c);
            return  c;
        }).collect(Collectors.toList());
        //方式二
        for (BaseAttrValue baseAttrValue : attrValueList) {
              //补全info表的id字段
            baseAttrValue.setAttrId(baseAttrInfo.getId());
            baseAttrValueMapper.insert(baseAttrValue);

        }
        //重新填充value数据
        baseAttrInfo.setAttrValueList(attrValueList);
        return baseAttrInfo;
    }
    /**
     * 通过平台属性id查询平台属性值的列表信息
     * @param id
     * @return
     */
    @Override
    public List<BaseAttrValue> getValueById(Long id) {
        //判断参数
        if(id==null){
            throw new RuntimeException("平台属性未选择,无法查询属性值信息");
        }
        //条件构造
        LambdaQueryWrapper<BaseAttrValue> wrapper = new LambdaQueryWrapper<>();
        //执行查询，获取结果
        List<BaseAttrValue> baseAttrValues = baseAttrValueMapper.selectList(wrapper);
        //返回
        return baseAttrValues;
    }
    /**
     * 分页查询SpuInfo的信息
     * @param category3Id
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<SpuInfo> getSpuInfoList(Long category3Id, Integer page, Integer size) {
        //条件构造器
        LambdaQueryWrapper<SpuInfo> wrapper = new LambdaQueryWrapper<>();
        //避免空指针
        if(category3Id!=null){
            wrapper.eq(SpuInfo::getCategory3Id,category3Id);
        }
         //执行查询
        IPage<SpuInfo> spuInfoIPage=spuInfoMapper.selectPage(new Page<>(page,size),wrapper);

        return  spuInfoIPage;
    }

    @Override
    public IPage<BaseTrademark> getTrademarkList(Integer page, Integer size) {
    /**
     * 分页查询品牌信息
     * @param page
     * @param size
     * @return
     */
        return baseTrademarkMapper.selectPage(new Page<>(page,size),null);
    }
    /**
     * 查询所有销售属性信息
     * @return
     */
    @Override
    public List<BaseSaleAttr> getBaseSaleAttr() {

        return baseSaleAttrMapper.selectList(null);
    }
    /**
     * 查询所有品牌信息
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademark() {
        return baseTrademarkMapper.selectList(null);
    }

    /**
     * 保存spu
     * @param spuInfo
     * @return
     */
    @Override
    public SpuInfo saveSpuInfo(SpuInfo spuInfo) {
        //保存spu信息
        spuInfoMapper.insert(spuInfo);
        //获取spu的id
        Long id = spuInfo.getId();
        //保存spu图片信息
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        List<SpuImage> dbImage=spuImageList.stream().map(i->{
           //补全spuId
           i.setSpuId(id);
           //添加图片
            spuImageMapper.insert(i);
            return i;
        }).collect(Collectors.toList());
        spuInfo.setSpuImageList(dbImage);
        //保存spu的销售属性信息
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        List<SpuSaleAttr> newSpuSaleAttrList = spuSaleAttrList.stream().map(s -> {
            //获取销售属性值
            List<SpuSaleAttrValue> spuSaleAttrValueList = s.getSpuSaleAttrValueList();
            //补全spuid
            s.setSpuId(id);
            //添加销售属性
            spuSaleAttrMapper.insert(s);
            //添加销售属性值
           List<SpuSaleAttrValue> newSpuSaleAttrValueList= spuSaleAttrValueList.stream().map(v->{
               //补全信息spuid
               v.setSpuId(id);
               //销售属性名称
               v.setSaleAttrName(s.getSaleAttrName());
               //添加销售属性值
               spuSaleAttrValueMapper.insert(v);
               return  v;
           }).collect(Collectors.toList());
           s.setSpuSaleAttrValueList(newSpuSaleAttrValueList);
           return s;
        }).collect(Collectors.toList());
        spuInfo.setSpuSaleAttrList(newSpuSaleAttrList);
        return spuInfo;
    }

    @Override
    public void addBaseTrademark(BaseTrademark baseTrademark) {
       baseTrademarkMapper.insert(baseTrademark);
    }

    @Override
    public BaseTrademark getBaseTrademarkById(Long id) {
        BaseTrademark baseTrademark = baseTrademarkMapper.selectById(id);
        return baseTrademark;
    }

    @Override
    public void editBaseTrademark(BaseTrademark baseTrademark) {
        baseTrademarkMapper.updateById(baseTrademark);
    }

    @Override
    public void deleteBaseTrademark(Long id) {
        baseTrademarkMapper.deleteById(id);
    }

    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        //拼接条件
        LambdaQueryWrapper<SpuImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpuImage::getSpuId,spuId);
        //查询列表
        List<SpuImage> spuImages = spuImageMapper.selectList(wrapper);
        return spuImages;
    }
    /**
     * 查询销售属性列表
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        List<SpuSaleAttr> spuSaleAttrs = spuSaleAttrMapper.selectSpuSaleAttrlist(spuId);
        return spuSaleAttrs;
    }

    /**
     * 保存sku
     * @param skuInfo
     * @return
     */
    @Override
    public SkuInfo saveSkuInfo(SkuInfo skuInfo) {
        //保存skuinfo数据
        skuInfoMapper.insert(skuInfo);
        //获取skuid
        Long skuId = skuInfo.getId();
        //保存skuImage数据
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();
        List<SkuImage> newSkuImageList =
                skuImageList.stream().map(image -> {
                    //补全数据
                    image.setSkuId(skuId);
                    //新增sku图片
                    skuImageMapper.insert(image);
                    return image;
                }).collect(Collectors.toList());
        skuInfo.setSkuImageList(newSkuImageList);
        //保存skuAttrValue数据
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();
        List<SkuAttrValue> newSkuAttrValueList = skuAttrValueList.stream().map(attrValue -> {
            //补全数据
            attrValue.setSkuId(skuId);
            //新增数据
            skuAttrValueMapper.insert(attrValue);
            return attrValue;
        }).collect(Collectors.toList());
        skuInfo.setSkuAttrValueList(newSkuAttrValueList);
        //保存skuSaleAttrValue数据
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        List<SkuSaleAttrValue> newSkuSaleAttrValueList = skuSaleAttrValueList.stream().map(sale -> {
            //补全spu
            sale.setSpuId(skuInfo.getSpuId());
            //补全sku
            sale.setSkuId(skuId);
            //新增
            skuSaleAttrValueMapper.insert(sale);
            return sale;
        }).collect(Collectors.toList());
        skuInfo.setSkuSaleAttrValueList(newSkuSaleAttrValueList);
        //返回结果
        return skuInfo;
    }
    /***
     * 分页查询skuinfo信息
     * @param page
     * @param size
     * @return
     */
    @Override
    public IPage<SkuInfo> list(Integer page, Integer size) {
        //分页查询skuinfo信息，并按照id降序
        IPage iPage = skuInfoMapper.selectPage(new Page<>(page,size), null);
        return iPage;
    }
    /**
     * sku商品上架/下架
     * @param skuId
     * @param status
     */
    @Override
    public void UpOrDown(Long skuId, Integer status) {
        //查询skuinfo的信息
        SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
        //若为空则直接失败
        if(skuInfo == null || skuInfo.getId() == null){
            throw new RuntimeException("商品不存在,无法进行上下架操作");
        }
        //设置skuinfo的状态
        skuInfo.setIsSale(status);
        //修改skuinfo的上下架状态
        skuInfoMapper.updateById(skuInfo);
    }

}
